<?php

class SiteCurrencyConfig extends DataExtension {
    private static $default_currency = 'USD';

    private static $db = array('SiteCurrency' => 'Varchar(3)');

    function populateDefaults() {
        $this->owner->SiteCurrency = Config::inst()->get($this->owner->class, 'default_currency', Config::FIRST_SET);
    }

    function updateCMSFields(FieldList $fields) {
        $fields->addFieldToTab("Root.Main", CurrencyDropdownField::create('SiteCurrency', _t('SiteCurrencyConfig.SITE_CURRENCY', 'Site Currency')));
    }

    static function current_site_currency() {
        if(DB::isActive() && ClassInfo::hasTable('SiteConfig') && !DB::getConn()->isSchemaUpdating()){
            $currency = SiteConfig::current_site_config()->SiteCurrency;
            if($currency) {
                return $currency;
            }
        }
        return Config::inst()->get('SiteCurrencyConfig', 'default_currency');
    }

}
